package com.xjrsoft.module.excel.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.ExcelExportUtil;
import com.xjrsoft.module.excel.dto.ExcelExportDto;
import com.xjrsoft.module.excel.dto.GetListExcelExportDto;
import com.xjrsoft.module.excel.entity.XjrExcelExport;
import com.xjrsoft.module.excel.service.IXjrExcelExportService;
import com.xjrsoft.module.excel.vo.ExcelExportVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:湘北智造-框架开发组
 * @Date:2020/11/10
 * @Description:excel导出控制器
 */
@AllArgsConstructor
@RestController
@RequestMapping("/excel-export")
@Api(value = "/excel-export",tags = "excel导出模块")
public class XjrExcelExportController {

    private IXjrExcelExportService excelExportService;

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/10
     * @Param:[dto, fModuleId]
     * @return:com.xjrsoft.common.result.Response
     * @Description:获取excel导出分页数据
     */
    @GetMapping()
    @ApiOperation(value = "获取excel导出分页数据")
    public Response<PageOutput<ExcelExportVo>> queryExcelExport(GetListExcelExportDto dto) {
        return Response.ok(excelExportService.getPageData(dto));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/10
     * @Param:[]
     * @return:voide
     * @Description:获取导出配置详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取导出配置详情")
    public Response toUpdatePage(@PathVariable String id) {
        XjrExcelExport xjrExcelExport = excelExportService.getById(id);
        if (xjrExcelExport != null) {
            return Response.ok(BeanUtil.copy(xjrExcelExport, ExcelExportVo.class));
        } else {
            return Response.notOk();
        }
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/10
     * @Param:[xjrExcelExport]
     * @return:com.xjrsoft.common.result.Response
     * @Description:excel导出按钮的新增
     */
    @PostMapping()
    @ApiOperation(value = "excel导出按钮的新增")
    public Response save(@RequestBody ExcelExportDto dto) {
        return Response.status(excelExportService.saveExcelExport(dto));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/10
     * @Param:[id]
     * @return:com.xjrsoft.common.result.Response
     * @Description:更新
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "更新")
    public Response update(@PathVariable String id, @RequestBody ExcelExportDto dto) {
        return Response.status(excelExportService.updateExcelExport(id, dto));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/10
     * @Param:[id]
     * @return:com.xjrsoft.common.result.Response
     * @Description:删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public Response deleteExcelExport(@PathVariable String id) {
        return Response.status(excelExportService.removeById(id));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/10
     * @Param:[id]
     * @return:com.xjrsoft.common.result.Response
     * @Description:启动
     */
    @PatchMapping("/start/{id}")
    @ApiOperation(value = "启动")
    public Response start(@PathVariable String id) {
        XjrExcelExport xjrExcelExport = new XjrExcelExport();
        xjrExcelExport.setEnabledMark(1);
        return Response.status(excelExportService.updateById(xjrExcelExport));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/10
     * @Param:[id]
     * @return:com.xjrsoft.common.result.Response
     * @Description:停止
     */
    @PatchMapping("/stop/{id}")
    @ApiOperation(value = "停止")
    public Response stop(@PathVariable String id) {
        XjrExcelExport xjrExcelExport = new XjrExcelExport();
        xjrExcelExport.setEnabledMark(0);
        return Response.status(excelExportService.updateById(xjrExcelExport));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/11
     * @Param:[titleDataList:标题列数据, dataList:导出数据List]
     * @return:com.xjrsoft.common.result.Response
     * @Description:导出excel文件
     */
    @SneakyThrows
    @PostMapping("/export-file")
    @ApiOperation(value = "导出excel文件")
    public Response exportExcel(String titleDataList, String dataList) {
        Map<String, String> titleData = (Map<String, String>) JSON.parse(titleDataList);
        if (StringUtils.isNotBlank(dataList)) {
            ArrayList<Map<String, Object>> list = JSON.parseObject(dataList, new TypeReference<ArrayList<Map<String, Object>>>() {
            });
            File file = new File(ExcelExportUtil.getFilePath());
            return Response.ok(ExcelExportUtil.saveFile(titleData, list, file));
        }
        return Response.status(false);
    }

    /**
    * @Author:湘北智造-框架开发组
    * @Date:2020/11/11
    * @Param:[url:请求地址url, urlParam:请求url携带的参数, titleHeader:excel列名]
    * @return:void
    * @Description:根据传过来的方法名和参数 请求接口获取数据生成excel
    */
    @SneakyThrows
    @PostMapping("/export-file-by")
    @ApiOperation(value = "根据传过来的方法名和参数 请求接口获取数据生成excel")
    public Response exportExcelBy(String url, String urlParam, String titleHeader) {
        String urlParamStr = urlParam.replace(" ", "&");
        String requestUrl = urlParamStr + "?" + urlParam;
        // 标题 String 转 Map
        Map<String, String> titleData = (Map<String, String>) JSON.parse(titleHeader);
        // 请求url获取数据
        String dataStr = HttpUtil.get(requestUrl);
        // 解析数据
        Map<String, Object> parse = (Map<String, Object>) JSON.parse(dataStr);
        List<Map<String, Object>> rowsList = (List<Map<String, Object>>) parse.get("rows");
        // 生成excel
        File file = new File(ExcelExportUtil.getFilePath());
        file = ExcelExportUtil.saveFile(titleData, rowsList, file);
        return Response.ok(file);
    }


}
